package cn.itcast.exercise

import java.io.{File, PrintWriter}

import scala.io.{BufferedSource, Source}

/**
  * @Package cn.itcast.exercise
  * @author KTL
  * @date 2021/5/21 0021 8:00
  * @version V1.0
  * @Copyright © 2015-04-29  One for each, and two for each
  *           DESC 从文件加载数据
  */
object WoudCount3 {
  def main(args: Array[String]): Unit = {

    val list: List[String] = Source.fromFile("data/input/words.txt").getLines().toList
    val result: Map[String, Int] = list.flatMap(t=>t.split(" "))
      .map(t=>(t,1))
      .groupBy(t=>t._1)
      .mapValues(t=>t.length)
    //通过java方式将文件输出文本文件
    val writer = new PrintWriter(new File("data/output/result.txt"))
    result.foreach(writer.println)
    writer.close()

  }

}
